package total

import (
	"fmt"
	"strconv"
	"time"

	"github.com/xuri/excelize"
)

// 初始化
func init() {
}

// Update 更新成交累计页
func Update(xlsx *excelize.File, totalIndex string, leaderIndex string, tTradeDate time.Time, workLine int, lastLine int, leaderDalianColummn, leaderZhengzhouColummn, leaderShangHaiColummn string) {
	// 临时变量
	var col string
	var style int
	var value string
	var iValue int
	var errstyle error
	var errv error

	fmt.Println("更新表格 ...")

	// 日期
	col = "A" + strconv.Itoa(workLine)
	xlsx.SetCellValue(totalIndex, col, tTradeDate)
	style, errstyle = xlsx.NewStyle(`{"number_format":14}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 所内领导 大连 总计
	col = leaderDalianColummn
	value = xlsx.GetCellValue(leaderIndex, col)
	col = "B" + strconv.Itoa(workLine)
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(totalIndex, col, iValue)
	} else {
		xlsx.SetCellValue(totalIndex, col, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}
	// 所内领导 郑州 总计
	col = leaderZhengzhouColummn
	value = xlsx.GetCellValue(leaderIndex, col)
	col = "C" + strconv.Itoa(workLine)
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(totalIndex, col, iValue)
	} else {
		xlsx.SetCellValue(totalIndex, col, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}
	// 所内领导 上海 总计
	col = leaderShangHaiColummn
	value = xlsx.GetCellValue(leaderIndex, col)
	col = "D" + strconv.Itoa(workLine)
	iValue, errv = strconv.Atoi(value)
	if errv == nil {
		xlsx.SetCellValue(totalIndex, col, iValue)
	} else {
		xlsx.SetCellValue(totalIndex, col, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 大连累计
	col = "E" + strconv.Itoa(workLine)
	value = "B" + strconv.Itoa(workLine) + "+E" + strconv.Itoa(lastLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 郑州累计
	col = "F" + strconv.Itoa(workLine)
	value = "C" + strconv.Itoa(workLine) + "+F" + strconv.Itoa(lastLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 上海累计
	col = "G" + strconv.Itoa(workLine)
	value = "D" + strconv.Itoa(workLine) + "+G" + strconv.Itoa(lastLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 三市合计
	col = "H" + strconv.Itoa(workLine)
	value = "E" + strconv.Itoa(workLine) + "+F" + strconv.Itoa(workLine) + "+G" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 交易日
	col = "I" + strconv.Itoa(workLine)
	value = "I" + strconv.Itoa(lastLine) + "+1"
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 大商所累计成交量占比
	col = "J" + strconv.Itoa(workLine)
	value = "E" + strconv.Itoa(workLine) + "/H" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":10}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 大商所日均成交量
	col = "K" + strconv.Itoa(workLine)
	value = "ROUND(E" + strconv.Itoa(workLine) + "/I" + strconv.Itoa(workLine) + ",0)"
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 当日合计
	col = "L" + strconv.Itoa(workLine)
	value = "B" + strconv.Itoa(workLine) + "+C" + strconv.Itoa(workLine) + "+D" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 上期所累计成交量占比
	col = "M" + strconv.Itoa(workLine)
	value = "G" + strconv.Itoa(workLine) + "/H" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":10}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计  郑商所累计成交量占比
	col = "N" + strconv.Itoa(workLine)
	value = "F" + strconv.Itoa(workLine) + "/H" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":10}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 上期所日均成交量
	col = "O" + strconv.Itoa(workLine)
	value = "G" + strconv.Itoa(workLine) + "/I" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}

	// 成交量累计 郑商所日均成交量
	col = "P" + strconv.Itoa(workLine)
	value = "F" + strconv.Itoa(workLine) + "/I" + strconv.Itoa(workLine)
	xlsx.SetCellFormula(totalIndex, col, value)
	style, errstyle = xlsx.NewStyle(`{"number_format":3}`)
	if errstyle == nil {
		xlsx.SetCellStyle(totalIndex, col, col, style)
	}
}
